/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package accessor;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Kirimi
 */
public class WareHouseAccessor extends model.Model{
    public List<WareHouse> getAll(){
        List<WareHouse> retVal = new ArrayList<WareHouse>();
        WareHouse wareHouse = null;
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = getConnection();
            String sql = "Select tblWareHouse.*,tblFactory.name as factoryName from tblWareHouse"
            + " inner join tblFactory "
                    + " on tblWareHouse.factoryID = tblFactory.factoryID";
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while(rs.next()){
                wareHouse = new WareHouse();
                wareHouse.setWareHouseID(rs.getInt("wareHouseID"));
                wareHouse.setWareHouseName(rs.getString("name"));
                wareHouse.setFactoryName(rs.getString("factoryName"));
                
                retVal.add(wareHouse);
            }
            closeObject(rs);
            closeObject(ps);
            closeObject(conn);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return retVal;
    }
    
    
    public int updateWareHouse(WareHouse wareHouse){
        int retVal = 0;
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = getConnection();
            String sql = "Update tblWareHouse set name = ? Where wareHouseID = ?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, wareHouse.getWareHouseName());
            ps.setInt(2, wareHouse.getWareHouseID());
            retVal = ps.executeUpdate();
            
            closeObject(ps);
            closeObject(conn);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return retVal;
    }
    
    public WareHouse getWareHouseByID(int wareHouseID){
        WareHouse wareHouse = null;
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = getConnection();
            String sql = "Select tblWareHouse.*,tblFactory.name as factoryName from tblWareHouse "
                    + " inner join tblFactory "
                    + " on tblWareHouse.factoryID = tblFactory.factoryID"
                    + " where tblWareHouse.wareHouseID = ?";
            ps = conn.prepareStatement(sql);
            ps.setInt(1, wareHouseID);
            rs = ps.executeQuery();
            if(rs.next()){
                wareHouse = new WareHouse();
                wareHouse.setWareHouseID(rs.getInt("wareHouseID"));
                wareHouse.setWareHouseName(rs.getString("name"));
                wareHouse.setFactoryName(rs.getString("factoryName"));
            }
            closeObject(rs);
            closeObject(ps);
            closeObject(conn);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return wareHouse;
    }
}
